package com.atguigu.gmall.common.config.interceptor;

import com.atguigu.gmall.common.util.ReqUtil;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

/**
 * @Author:lyd
 **/
@Component
public class RequestHeaderInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate template) {
//        HttpServletRequest httpServletRequest = CartController.requestThreadLocal.get();
//        if (httpServletRequest != null) {
//            String header = httpServletRequest.getHeader("Cookie");
//            template.header("Cookie", header);
//        }
        HttpServletRequest request = ReqUtil.getOldRequest();
        if (request == null) {
            return;
        }
        Enumeration<String> headerNames = request.getHeaderNames();
        // 遍历头信息
        while (headerNames.hasMoreElements()) {
            String headerName = headerNames.nextElement();
            if ("Cookie".equalsIgnoreCase(headerName) ||
                    "userTempId".equalsIgnoreCase(headerName) ||
                    "token".equals(headerName)) {
                String headerValue = request.getHeader(headerName);
                template.header(headerName, headerValue);
            }

        }
    }
}
